package com.zhuhjay.ruiji.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhuhjay.ruiji.common.R;
import com.zhuhjay.ruiji.domain.Setmeal;
import com.zhuhjay.ruiji.domain.dto.SetmealDto;
import com.zhuhjay.ruiji.service.ICategoryService;
import com.zhuhjay.ruiji.service.ISetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ZhuHJay
 * @date 2022/5/16 21:36
 */
@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    private final ISetmealService setmealService;
    private final ICategoryService categoryService;
    public SetmealController(ISetmealService setmealService, ICategoryService categoryService) {
        this.setmealService = setmealService;
        this.categoryService = categoryService;
    }

    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto){
        setmealService.saveSetmealAndDish(setmealDto);
        return R.success("添加成功");
    }

    @GetMapping("/page")
    public R<Page<SetmealDto>> page(int page, int pageSize, String name){
        // 查询套餐的信息
        Page<Setmeal> setmealPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.like(StringUtils.isNotEmpty(name), Setmeal::getName, name)
                        .orderByDesc(Setmeal::getUpdateTime);
        setmealService.page(setmealPage, setmealLambdaQueryWrapper);

        // 获取更多的细节数据
        Page<SetmealDto> setmealDtoPage = new Page<>();
        // 拷贝数据
        BeanUtils.copyProperties(setmealPage, setmealDtoPage, "records");
        // 获取关联数据以及菜品分类数据
        List<SetmealDto> setmealDtoList = setmealPage.getRecords().stream().map(setmeal -> {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(setmeal, setmealDto);
            // 根据菜品分类id查询该套餐的菜品分类名称
            setmealDto.setCategoryName(categoryService.getById(setmeal.getCategoryId()).getName());
            return setmealDto;
        }).collect(Collectors.toList());
        setmealDtoPage.setRecords(setmealDtoList);

        return R.success(setmealDtoPage);
    }

    @GetMapping("/{id}")
    public R<SetmealDto> query(@PathVariable("id") Long id){
        SetmealDto setmealDto = setmealService.querySetmaelAndDish(id);
        return R.success(setmealDto);
    }

    @PutMapping
    public R<String> update(@RequestBody SetmealDto setmealDto){
        setmealService.updateSetmealAndDish(setmealDto);
        return R.success("更新成功");
    }

    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids){
        setmealService.deleteSetmealAndDish(ids);
        return R.success("删除成功");
    }

    @PostMapping("/status/{status}")
    public R<String> enableStatus(@PathVariable("status") int status, @RequestParam List<Long> ids){
        setmealService.update(new LambdaUpdateWrapper<Setmeal>()
                .in(Setmeal::getId, ids)
                .set(Setmeal::getStatus, status));
        return R.success("修改成功");
    }

    @GetMapping("list")
    public R<List<Setmeal>> list(Setmeal setmeal){
        return R.success(setmealService.list(new LambdaQueryWrapper<Setmeal>()
                .eq(setmeal.getCategoryId() != null, Setmeal::getCategoryId, setmeal.getCategoryId())
                .eq(setmeal.getStatus() != null, Setmeal::getStatus, setmeal.getStatus())
                .orderByDesc(Setmeal::getUpdateTime)));
    }

}
